In [1]:
import torch 
import torchvision
import torch.nn as nn
import torch.utils.data as data
import numpy as np
import torchvision.transforms as transforms
import torchvision.datasets as dsets
from torch.autograd import Variable

Simple Example


In [2]:
# random normal
x = torch.randn(5, 3)
print (x)
x = torch.randm(5, 3)
prin


 0.1125 -0.1725 -1.4885
-1.3278 -2.5332  0.3914
-0.1966 -1.2831 -0.5606
 0.3959  0.6616  1.6949
 1.3265 -0.0778  0.7417
[torch.FloatTensor of size 5x3]


In [3]:
# build a layer
linear = nn.Linear(3, 2)

In [4]:
# Sess weight and bias
print (linear.weight)
print (linear.bias)


Parameter containing:
 0.3884 -0.3335 -0.5146
-0.3692  0.1977 -0.4081
[torch.FloatTensor of size 2x3]

Parameter containing:
-0.4826
-0.0038
[torch.FloatTensor of size 2]


In [5]:
# forward propagate
y = linear(Variable(x))
print (y)


Variable containing:
-1.0986 -0.1575
-2.0311  0.4378
-0.6131  1.3938
 0.6790  0.7929
-0.3134  0.8351
[torch.FloatTensor of size 5x2]

Convert numpy array to torch tensor


In [ ]:
# convert numpy array to tensor
a = np.array([[1,2], [3,4]])
b = torch.from_numpy(a)
print (b)

Input pipeline

(1) Preprocessing


In [18]:
# Image Preprocessing 
transform = transforms.Compose([
    transforms.Scale(40),
    transforms.RandomHorizontalFlip(),
    transforms.RandomCrop(32),
    transforms.ToTensor()])

(2) Define Dataset


In [14]:
# download and loading dataset f
train_dataset = dsets.CIFAR10(root='./data/',
                               train=True, 
                               transform=transform,
                               download=True)

image, label = train_dataset[0]
print (image.size())
print (label)


Files already downloaded and verified
torch.Size([3, 32, 32])
6

(3) Data Loader


In [16]:
# data loader provides queue and thread in a very simple way
train_loader = data.DataLoader(dataset=train_dataset,
                               batch_size=100, 
                               shuffle=True,
                               num_workers=2)

In [ ]:
# iteration start then queue and thread start
data_iter = iter(train_loader)

# mini-batch images and labels
images, labels = data_iter.next()

for images, labels in train_loader:
    # your training code will be written here
    pass

(4) What about custom dataset not cifar10?


In [25]:
class CustomDataset(data.Dataset):
    def __init__(self):
        pass
    def __getitem__(self, index):
        # You should build this function to return one data for given index
        pass
    def __len__(self):
        pass

In [26]:
custom_dataset = CustomDataset()
data.DataLoader(dataset=custom_dataset,
                batch_size=100, 
                shuffle=True,
                num_workers=2)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-a76c7b5c92c3> in <module>()
      3                 batch_size=100,
      4                 shuffle=True,
----> 5                 num_workers=2)

/home/yunjey/anaconda3/lib/python3.5/site-packages/torch/utils/data/dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, num_workers, collate_fn, pin_memory)
    250             self.sampler = sampler
    251         elif shuffle:
--> 252             self.sampler = RandomSampler(dataset)
    253         elif not shuffle:
    254             self.sampler = SequentialSampler(dataset)

/home/yunjey/anaconda3/lib/python3.5/site-packages/torch/utils/data/sampler.py in __init__(self, data_source)
     45 
     46     def __init__(self, data_source):
---> 47         self.num_samples = len(data_source)
     48 
     49     def __iter__(self):

TypeError: 'NoneType' object cannot be interpreted as an integer

Using Pretrained Model


In [17]:
# Download and load pretrained model
resnet = torchvision.models.resnet18(pretrained=True)


Downloading: "https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth" to /home/yunjey/.torch/models/resnet18-5c106cde.pth
100%|██████████| 46827520/46827520 [07:48<00:00, 99907.53it/s] 

In [ ]:
# delete top layer for finetuning
sub_model = nn.Sequentialtial(*list(resnet.children()[:-1]))

# for test
images = Variable(torch.randn(10, 3, 256, 256))
print (resnet(images).size())
print (sub_model(images).size())

Save and Load Model


In [ ]:
# Save and load the trained model
torch.save(sub_model, 'model.pkl')

model = torch.load('model.pkl')